import { uniapp } from '@/utils/uni'
import request from '../encrypt/request'

// 第三方登录
export const threePartiesLogin = (params: any) => {
  return request.loginRequest(
    {},
    '/sso/threePartiesLogin',
    '/sso/threePartiesLogin',
    'POST',
    {
      ...params,
      appCode: '650116',
    }
  )
}

//未绑定用户手机号
export const firstLoginAndBind = (params: any) => {
  return request.loginRequest(
    {},
    '/sso/weChatMiniLoginAndBind',
    '/sso/weChatMiniLoginAndBind',
    'POST',
    params
  )
}

// 根据tgt获取用户信息
//  cert/certInfo
export const accountUserInfo = (params: string) => {
  const newHeader = {
    token: params,
    'eshimin-version': '1.2.1.4',
    'system-version': 9,
    'content-type': 'application/x-www-form-urlencoded',
  }
  const data = {}
  return request.loginRequest(
    newHeader,
    '/account/user/info',
    '/account/user/info',
    'GET',
    data
  )
}

//  验证身份证是否已绑定
export const verifyIdCard = (data: { name: string; idcard: string }) => {
  const newHeader = {
    token: uni.getStorageSync('threePartLoginData')?.tgt || '',
    'eshimin-version': '1.2.1.4',
    'system-version': 9,
    'content-type': 'application/x-www-form-urlencoded',
  }

  return request.loginRequest(
    newHeader,
    '/account/account/idcard/check',
    '/account/account/idcard/check',
    'POST',
    data
  )
}

// 获取慧眼biztoken
export const getBizToken = (data: { name: string; idcardNo: string }) => {
  const newHeader = {
    token: uni.getStorageSync('threePartLoginData')?.tgt || '',
    'eshimin-version': '1.2.1.4',
    'system-version': 9,
    'content-type': 'application/x-www-form-urlencoded',
  }

  return request.loginRequest(
    newHeader,
    '/identify/tencent/getBizToken',
    '/identify/tencent/getBizToken',
    'POST',
    data
  )
}

// /identify/tencent/identify/result
// 提交biztoken进行实名
export const postBizToken = (data: { bizToken: string }) => {
  const newHeader = {
    token: uni.getStorageSync('threePartLoginData')?.tgt || '',
    'eshimin-version': '1.2.1.4',
    'system-version': 9,
    'content-type': 'application/x-www-form-urlencoded',
  }

  return request.loginRequest(
    newHeader,
    '/identify/tencent/identify/result',
    '/identify/tencent/identify/result',
    'POST',
    data
  )
}

// 刷脸修改绑定手机号
export const modifyBindMobile = (data: {
  bizToken: string
  idcard: string
  newPhoneNum: string
}) => {
  const newHeader = {
    token: uni.getStorageSync('threePartLoginData')?.tgt || '',
    'eshimin-version': '1.2.1.4',
    'system-version': 9,
    'content-type': 'application/x-www-form-urlencoded',
  }

  return request.loginRequest(
    newHeader,
    '/identify/tencent/mobile/replace',
    '/identify/tencent/mobile/replace',
    'POST',
    data
  )
}
//获取openid
export const getOpenid = () => {
  const newHeader = {
    token: uni.getStorageSync('threePartLoginData')?.tgt || '',
    'eshimin-version': '1.2.1.4',
    'system-version': 9,
    'content-type': 'application/x-www-form-urlencoded',
    userId: uni.getStorageSync('userInfo')?.aid || '',
    platform: 'WEIXINMINI',
  }

  return request.loginRequest(
    newHeader,
    '/account/thirdPlatform/openId',
    '/account/thirdPlatform/openId',
    'GET',
    {
      userId: uni.getStorageSync('userInfo')?.aid || '',
      platform: '微信小程序',
    }
  )
}

// 更换绑定用户
export const bindAccountSys = async () => {
  const newHeader = {
    token: uni.getStorageSync('threePartLoginData')?.tgt || '',
    'eshimin-version': '1.2.1.4',
    'system-version': 9,
    'content-type': 'application/x-www-form-urlencoded',
  }
  // uniapp 微信授权
  const { code: authCode } = await uniapp('login')

  return request.loginRequest(
    newHeader,
    '/account/thirdPlatform/binding',
    '/account/thirdPlatform/binding',
    'POST',
    {
      userId: uni.getStorageSync('userInfo')?.aid || '',
      platformType: 'WEIXIN',
      authCode,
    }
  )
}

// 支付宝绑定用户
export const alipayMiniBind = async (params: {
  mobile: string
  openId: string
  code: string
  userId: string
}) => {
  return request.loginRequest(
    {},
    '/sso/alipayMiniBind',
    '/sso/alipayMiniBind',
    'POST',
    {
      isSmaliProgram: 1,
      ...params,
    }
  )
}

// 支付宝： 手机号获取验证码
export const sendSMS = (params: { mobile: string }) => {
  return request.loginRequest(
    {},
    '/account/sms/sendCode',
    '/account/sms/sendCode',
    'POST',
    {
      isSmaliProgram: 1,
      ...params,
    }
  )
}
